import { NextResponse } from 'next/server';
import articlesModule from '@/modules/article';
import generateSlugify from "@/utils/generateSlugify";
import { TIP_TAP_DEFAULT } from '@/config/settings'

export async function GET(request) {
  const codeMap = { 0: '获取成功', 1: '内容为空', };
  const { searchParams } = new URL(request.url);
  const page = searchParams.get('page') ? Number(searchParams.get('page')) : 1;
  const limit = searchParams.get('limit') ? Number(searchParams.get('limit')) : 10;
  const offset = (page - 1) * limit;
  const articles = await articlesModule.get(limit, offset);
  if (!articles) { return NextResponse.json({ code: 1, codeMap, message: 'No articles found' }); }
  return NextResponse.json({ code: 0, codeMap, message: 'Success', data: articles.articles, total: articles.total, });
}

export async function POST(request) {
  const codeMap = { 0: '创建成功', 1: '标题不能为空', 2: '创建失败' };
  const defaultContent = JSON.stringify(TIP_TAP_DEFAULT);
  const { title, description } = await request.json();
  if (!title) { return NextResponse.json({ code: 1, codeMap, message: 'Title cannot be empty' }); }
  const article = await articlesModule.create({ slug : null, title, description, content : defaultContent, template: 'default', status : 1, cover : '', sort : 0, meta_title : '', meta_description : '', blog_id : null });
  if (!article) { return NextResponse.json({ code: 2, codeMap, message: 'Create failed' }); }
  return NextResponse.json({ code: 0, codeMap, message: 'Success', id: article.insertId });
}

export async function PUT(request) {
  const codeMap = { 0: '更新成功', 1: '文章未找到', 2: '标题不能为空' };
  const { id, slug: originSlug, title, description, content: originContent, template, status, cover, sort, meta_title, meta_description, blog_id } = await request.json();
  const article = await articlesModule.getById(Number(id));
  if (!article) { return NextResponse.json({ code: 1, codeMap, message: 'No articles found' }); }
  if (!title) { return NextResponse.json({ code: 2, codeMap, message: 'Title cannot be empty' }); }
  const content = originContent ? JSON.stringify(originContent) : JSON.stringify(TIP_TAP_DEFAULT);
  const slugList = await articlesModule.getSlugs();
  const slug = !originSlug ? await generateSlugify(title, slugList) : originSlug;
  const result = await articlesModule.updateById(id, { slug, title, description, content, template, status, cover, sort, meta_title, meta_description, blog_id });
  if (result.affectedRows === 0) { return NextResponse.json({ code: 1, codeMap, message: 'Update failed' }); }
  const newArticle = await articlesModule.getById(Number(id));
  return NextResponse.json({ code: 0, codeMap, message: 'Success', data: newArticle });
}

export async function DELETE(request) {
  const { id } = await request.json();
  const codeMap = { 0: '删除成功', 1: '文章不存在', 2: "删除失败" };
  const isHas = await articlesModule.getById(id);
  if (!isHas) { return NextResponse.json({ code: 1, codeMap, message: 'No articles found' }); }
  const result = await articlesModule.deleteById(id);
  if (result.affectedRows === 0) { return NextResponse.json({ code: 2, codeMap, message: 'Delete failed' }); }
  return NextResponse.json({ code: 0, codeMap, message: 'Delete success' });
}